package com.base.library.http;


import io.reactivex.Observable;

/**
 * Created by SupLuo on 2016/5/25.
 */
public class ApiUtil {
    /**
     * 统一处理Http请求结果
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> Observable<T> flatResult(HttpResult<T> result) {
        return Observable.create(subscriber -> {
            switch (result.code) {
                case 0:
                    if (result.data != null) {
                        subscriber.onNext(result.data);
                    } else {
                        subscriber.onNext((T) new String(""));
                    }
                    break;
                default:
                    subscriber.onError(new ApiException(result));
                    break;
            }
            subscriber.onComplete();
        });
    }
}
